import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 * @author Simon
 *
 */
public class ChartPanel extends JPanel 
{
	private final int YAXIS = 250;
	private final int XAXIS = 10;
	private boolean selectionMade;
	private boolean reset;
	
	private String firstInitials;
	private String firstName;
	private int firstPapers;
	private int firstCitations;
	
	private String secondInitials;
	private String secondName;
	private int secondPapers;
	private int secondCitations;
	
	private int firstBar;
	private int secondBar;
	private int thirdBar;
	private int fourthBar;
	private int fifthBar;
	private int sixthBar;
	
	
	public ChartPanel() 
	{
		this.setBorder(BorderFactory.createLineBorder(Color.black));
		this.setBackground(Color.WHITE);
		this.selectionMade = false;
	}

	public void resize()
	{
		if(firstBar > YAXIS - 15 || secondBar > YAXIS - 15)
		{
			firstBar = (firstBar * 3)/4;
			secondBar = (secondBar * 3)/4;
			resize();
		}
		
		if(thirdBar > YAXIS - 15 || fourthBar > YAXIS - 15)
		{
			thirdBar = (thirdBar * 3)/4;
			fourthBar = (fourthBar * 3)/4;
			resize();
		}
	}

	public Dimension getPreferredSize() 
	{
		return new Dimension(500,480);
	}

	public void paintComponent(Graphics g) 
	{
		if(reset)
		{}
		else if(selectionMade)
		{
			int one = firstPapers;
			int two = secondPapers;
			int three = firstCitations;
			int four = secondCitations;
			double five =0;
			double six = 0;
			
			/*
			 * TODO Fix this hack
			 */
			
			if(one >0 && two > 0)
			{
				five = three/one;
				six = four/two;
			}
			
			else if(two == 0 && one > 0)
			{
				six = 0;
				five = three/one;
			}
			else if(one == 0 && two > 0)
			{
				five = 0;
				six = four/two;
			}
			else
			{
				five = 0;
				six = 0;
			}
			
			resize();
			int offset = 55;
			int width = 50;
			
			super.paintComponent(g);
			
			g.setFont(new Font("TimesRoman", Font.BOLD, 14));
			g.setColor(Color.BLACK);
			g.drawString("Total Papers Produced                                     Total Citations",XAXIS+25,YAXIS-232);
			g.setFont(new Font("TimesRoman", Font.PLAIN, 9));
			
			g.drawLine(XAXIS, YAXIS, XAXIS + 450, YAXIS);
			g.setColor(Color.BLUE);
			g.fillRect(XAXIS + 20,YAXIS -firstBar,width, firstBar);
			g.drawString(firstInitials,XAXIS+20, YAXIS + 20);
			g.drawString(firstName,XAXIS+20, YAXIS + 40);
			g.drawString(""+one,XAXIS+20, YAXIS + 60);

			g.setColor(Color.RED);
			offset = 2*offset;
			g.fillRect(XAXIS + offset,YAXIS - secondBar,width, secondBar);
			g.drawString(secondInitials,XAXIS+offset, YAXIS + 20);
			g.drawString(secondName,XAXIS+offset, YAXIS + 40);
			g.drawString(""+two,XAXIS+offset, YAXIS + 60);
			
			g.setColor(Color.BLUE);
			offset = 2*offset + 50;
			g.fillRect(XAXIS + offset,YAXIS - thirdBar,width, thirdBar);
			g.drawString(firstInitials,XAXIS+offset, YAXIS + 20);
			g.drawString(firstName,XAXIS+offset, YAXIS + 40);
			g.drawString(""+three,XAXIS+offset, YAXIS + 60);
			
			g.setColor(Color.RED);
			offset = offset + 90;
			g.fillRect(XAXIS + offset,YAXIS - fourthBar,width, fourthBar);
			g.drawString(secondInitials,XAXIS+offset, YAXIS + 20);
			g.drawString(secondName,XAXIS+offset, YAXIS + 40);
			g.drawString(""+four,XAXIS+offset, YAXIS + 60);
			
			g.setColor(Color.BLACK);
			g.setFont(new Font("TimesRoman", Font.BOLD, 14));
			
			String firstString = firstInitials+" "+firstName+" has approximately "+five+" citations per paper produced.";
			String secondString = secondInitials+" "+secondName+" has approximately "+six+" citations per paper produced.";
			int onLine = YAXIS+85;
			onLine += 20;
			
			if(firstString.length()>70)
			{
				g.drawString(firstString.substring(0, 70)+"-",XAXIS, onLine);
				String tmp = firstString.substring(70);
				while(tmp.length()>70)
				{
					onLine+=20;
					g.drawString(tmp,XAXIS, onLine);
					tmp = tmp.substring(70);
				}
				onLine += 20;
				g.drawString(tmp,XAXIS, onLine);
			}
			else
			{
				g.drawString(firstString,XAXIS, onLine);
			}
			onLine += 20;
			
			if(secondString.length()>70)
			{
				
				g.drawString(secondString.substring(0, 70)+"-",XAXIS, onLine);
				String tmp = secondString.substring(70);
				while(tmp.length()>70)
				{
					onLine+=20;
					g.drawString(tmp,XAXIS, onLine);
					tmp = tmp.substring(70);
				}
				onLine += 20;
				g.drawString(tmp,XAXIS, onLine);
				
			}
			else
			{
				g.drawString(secondString,XAXIS, onLine);
			}
			//g.drawString(firstInitials+" "+firstName+" has approximately "+five+" citations per paper produced.", XAXIS, YAXIS+85);
			//g.drawString(secondInitials+" "+secondName+" has approximately "+six+" citations per paper produced.", XAXIS, YAXIS+105);
			//System.out.println(firstInitials+" "+firstName+" ratio: "+five+" cites per paper");
			//System.out.println(secondInitials+" "+secondName+" ratio: "+six+" cites per paper");
		}

	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}

	public String getSecondName() {
		return secondName;
	}

	public void setFirstPapers(int firstPapers) {
		this.firstPapers = firstPapers;
		this.firstBar = firstPapers;
	}

	public int getFirstPapers() {
		return firstPapers;
	}

	public void setSecondPapers(int secondPapers) {
		this.secondPapers = secondPapers;
		this.secondBar = secondPapers;
	}

	public int getSecondPapers() {
		return secondPapers;
	}

	public void setFirstCitations(int firstCitations) 
	{
		this.firstCitations = firstCitations;
		this.thirdBar = firstCitations;
	}

	public int getFirstCitations() {
		return firstCitations;
	}

	public void setSecondCitations(int secondCitations) 
	{
		this.secondCitations = secondCitations;
		this.fourthBar = secondCitations;
	}

	public int getSecondCitations() {
		return secondCitations;
	}

	/**
	 * @param b
	 */
	public void setSelectionMade(boolean b) 
	{
		this.selectionMade = b;	
	}

	public void setFirstInitials(String firstInitials) {
		this.firstInitials = firstInitials;
	}

	public String getFirstInitials() {
		return firstInitials;
	}

	public void setSecondInitials(String secondInitials) {
		this.secondInitials = secondInitials;
	}

	public String getSecondInitials() {
		return secondInitials;
	}
	
	public static void main(String [] args)
	{
		JFrame frame = new JFrame("Chart");
		ChartPanel cp = new ChartPanel();
		frame.add(cp);
		cp.setFirstInitials("DCU");
		cp.setSecondInitials("DIT");
		cp.setFirstCitations(5000);
		cp.setSecondCitations(3000);
		cp.setFirstName("School of Computing");
		cp.setSecondName("Dept of CS");
		cp.setFirstPapers(400);
		cp.setSecondPapers(280);
		frame.setVisible(true);
		cp.setSelectionMade(true);
		cp.repaint();
	}
}